<?php

class TestPeer extends BaseTestPeer
{
    public static function buildLevelArray($test, $questions, $level)
    {        
        switch ($level){
            case 1:
                $total_sum = $test->getAmountMarkLevel1();
                $total_count = $test->getAmountTaskLevel1();
                break;
            case 2:
                $total_sum = $test->getAmountMarkLevel2();
                $total_count = $test->getAmountTaskLevel2();
                break;
            case 3:
                $total_sum = $test->getAmountMarkLevel3();
                $total_count = $test->getAmountTaskLevel3();
                break;
        }
        foreach($questions as $question)
        {
            $question_arr[$question->getId()] = $question->getMark();
        }
        $result_arr = array();
        while (array_sum($result_arr) != $total_sum)
        {
            $result_arr = array();
            $keys = array_keys($question_arr);
            shuffle($keys);
            $new = array();
            foreach($keys as $key) {
                $new[$key] = $question_arr[$key];
            }
            $question_arr = $new;
            $i = 0;
            foreach($question_arr as $key => $q_arr)
            {
                $i++;
                $result_arr[$key] = $q_arr;
                if($i == $total_count-1) break;
            }
            $i = 0;
            foreach($question_arr as $key => $q_arr)
            {
                $i++;
                if($i > $total_count-1)
                {
                    if(array_sum($result_arr) + $q_arr == $total_sum)
                    {
                        $result_arr[$key] = $q_arr;
                        break;
                    }
                }
            }
        }
        return array_keys($result_arr);
    }
    public static function buildTestArray($id)
    {
        $test = TestPeer::retrieveByPK($id);

        $ids = array();
        $questions = $test->getQuestionsLevel(1);
        $ids = TestPeer::buildLevelArray($test, $questions, 1);       
        $questions = $test->getQuestionsLevel(2);
        $ids = array_merge ($ids, TestPeer::buildLevelArray($test, $questions, 2));
        $questions = $test->getQuestionsLevel(3);
        $ids = array_merge ($ids, TestPeer::buildLevelArray($test, $questions, 3));
        shuffle($ids);
        return $ids;
    }
    
     public static function doSelectForTeacher(Criteria $criteria, PropelPDO $con = null)
    {
        if ($con === null)
        {
            $con = Propel::getConnection(TestPeer::DATABASE_NAME, Propel::CONNECTION_READ);
        }
        if (!$criteria->hasSelectClause())
        {
            $criteria = clone $criteria;
            TestPeer::addSelectColumns($criteria);
        }
        if(!sfContext::getInstance()->getUser()->getGuardUser()->getIsSuperAdmin())
        {
            $criteria->addJoin(TestPeer::TEST_BLOCK_ID, TestBlockPeer::ID);
            $criteria->addJoin(TestBlockPeer::SUBJECT_ID, UserSubjectPeer::SUBJECT_ID);
            $criteria->addJoin(UserSubjectPeer::USER_ID, sfContext::getInstance()->getUser()->getGuardUser()->getId());
        }
        
        return TestPeer::doSelect($criteria);
    }

    public static function doCountForTeacher(Criteria $criteria, PropelPDO $con = null)
    {
        if ($con === null)
        {
            $con = Propel::getConnection(TestPeer::DATABASE_NAME, Propel::CONNECTION_READ);
        }
        if (!$criteria->hasSelectClause())
        {
            $criteria = clone $criteria;
            TestPeer::addSelectColumns($criteria);
        }
        if(!sfContext::getInstance()->getUser()->getGuardUser()->getIsSuperAdmin())
        {
            $criteria->addJoin(TestPeer::TEST_BLOCK_ID, TestBlockPeer::ID);
            $criteria->addJoin(TestBlockPeer::SUBJECT_ID, UserSubjectPeer::SUBJECT_ID);
            $criteria->addJoin(UserSubjectPeer::USER_ID, sfContext::getInstance()->getUser()->getGuardUser()->getId());
        }

        return TestPeer::doCount($criteria);
    }
}
